package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.mvc.Finally;

import com.aton.db.SessionFactory;

import models.Dealer;
import models.Shop;
import models.ShopStatus;
import models.mappers.DeviceMapper;
import models.mappers.ShopMapper;
import vos.ShopSearchVo;
import vos.ShopVo;

public class ShopService {
	/**
	 * 根据条件统计门店数量
	 *
	 * @Title: countShop 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countShop(ShopSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.countShop(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件查询门店列表
	 *
	 * @Title: listShop 
	 * @param vo
	 * @return
	 * @return: List<Shop>
	 */
	public static List<Shop> listShop(ShopSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.listShop(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param operatorid 
	 * @return: void
	 */
	public static Boolean betchEnable(List<String> listid, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param listid 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(List<String> listid,String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            mapper.betchDisable(map);
            DeviceMapper deviceMapper = ss.getMapper(DeviceMapper.class);
            deviceMapper.DisableByShops(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param listid
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除门店
	 *
	 * @Title: delShop 
	 * @param id 门店id
	 * @return
	 * @return: boolean
	 */
	public static boolean delShop(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
	        mapper.delShop(id);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 验证编号是否存在
	 *
	 * @Title: checkCode 
	 * @param shopid 门店编号
	 * @return
	 * @return: boolean
	 */
	public static boolean checkCode(String shopid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
	        String shop= mapper.checkCode(shopid);
	        if(StringUtils.isEmpty(shop)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取门店信息
	 *
	 * @Title: getById 
	 * @param shopid 门店id
	 * @return
	 * @return: Shop
	 */
	public static ShopVo getById(String shopid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.getById(shopid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取门店信息
	 *
	 * @Title: getById 
	 * @param shopid 门店id
	 * @return
	 * @return: Shop
	 */
	public static ShopVo getVoById(String shopid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.getVoById(shopid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存门店信息
	 *
	 * @Title: saveShop 
	 * @param shop 门店对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveShop(Shop shop) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			Date date=new Date();
			shop.createtime=date;
	        mapper.saveShop(shop);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新门店信息
	 *
	 * @Title: updateShop 
	 * @param shop
	 * @return
	 * @return: boolean
	 */
	public static boolean updateShop(Shop shop) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			Date date=new Date();
			shop.modifytime=date;
	        mapper.updateShop(shop);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据经销商查询门店
	 *
	 * @Title: selectByDealer 
	 * @param id
	 * @return
	 * @return: Dealer
	 */
	public static List<String> selectByDealer(String dealerid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.selectByDealer(dealerid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据经销商id集合查询门店集合
	 *
	 * @Title: selectByDealers 
	 * @param listid
	 * @return
	 * @return: List<Shop>
	 */
	public static List<String> selectByDealers(List<String> listid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.selectByDealers(map);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取视图
	 *
	 * @Title: getViewById 
	 * @param id
	 * @return
	 * @return: models.Shop
	 */
	public static ShopVo getViewById(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.getViewById(id);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取combobox下拉列表
	 *
	 * @Title: getCombobox 
	 * @return
	 * @return: List<Shop>
	 */
	public static List<Shop> getCombobox() {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
			return mapper.getCombobox();
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据经销商统计门店
	 *
	 * @Title: countShopByDealer 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countShopByDealer(ShopSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.countShopByDealer(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据经销商获取门店列表
	 *
	 * @Title: getShopByDealer 
	 * @param vo
	 * @return
	 * @return: List<Shop>
	 */
	public static List<Shop> getShopByDealer(ShopSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.getShopByDealer(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<ShopVo>
	 */
	public static List<ShopVo> exportXls(ShopSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 查看门店下拉列表
	 *
	 * @Title: getStatusCombobox 
	 * @return
	 * @return: List<ShopStatus>
	 */
	public static List<ShopStatus> getStatusCombobox() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			ShopMapper mapper = ss.getMapper(ShopMapper.class);
            return mapper.getStatusCombobox();
        } finally {
            ss.close();
        }
	}
}
